﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace MergeShapeFile
{
    public partial class MainFram : Form
    {
       // new Thread(MergeByThread).Start();
        Thread thread;
        string mergeFile;
        //string sourceDerectiony;
        string[] Resources;

        private delegate void UpdateState();
        public MainFram()
        {
            InitializeComponent();
            MergeShape.MergeShapeFiled += new EventHandler(On_MergeShapeFiled);
            MergeShape.MergeShapeFiling += new EventHandler(On_MergeShapeFiling);
        }

        private void btn_browse_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "Shape Files|*.shp";
            openFileDialog.Multiselect = true;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                this.tbFilePath.Text = openFileDialog.FileName;
                addItemsToListview(openFileDialog.FileNames);
                Resources = openFileDialog.FileNames;
                
               // Fileamount.Text=""
                this.Invoke(new EventHandler(delegate
                {

                    Fileamount.Text = "文件数量：" + lvFiles.Items.Count.ToString();

                }));
                prgMerge.Value = 0;
                prgMerge.Maximum = lvFiles.Items.Count;

            }
            openFileDialog.Dispose();
        }

        private void addItemsToListview(string[] filenames)
        {
            foreach (string fn in filenames)
            {
                ListViewItem lvi = new ListViewItem(fn);
                this.lvFiles.Items.Add(lvi);
            }
        }

        private void btnselect_Click(object sender, EventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.Filter = "Shape Files|*.shp";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                tbOutFilePath.Text = dialog.FileName;
            }
            dialog.Dispose();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            //this.lblPercent.Text = "";
            mergeFile = tbOutFilePath.Text.Trim();//合并源文件
            if (mergeFile.Length <= 0)
            {
                MessageBox.Show("SourceFolder or TargetFile is null.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            if (this.lvFiles.Items.Count == 1)
            {
                MessageBox.Show("Merge file less 1.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            btnpause.Enabled = true;
            btnStart.Enabled = false;
            thread = new Thread(StartMerge);
            thread.Start();
        }

        private void StartMerge()
        {
           MergeShape MergeShp = new MergeShape();
           MergeShp.mergeShp(Resources, mergeFile);
        }
        private void On_MergeShapeFiling(object sender, EventArgs e)
        {
            prgMerge.Invoke(new UpdateState(prgMerging));
            NowPath.Invoke(new UpdateState(nowPath));
            Finished.Invoke(new UpdateState(finished));
            
        }

        private void On_MergeShapeFiled(object sender, EventArgs e)
        {
            prgMerge.Invoke(new UpdateState(prgMerged));
            NowPath.Invoke(new UpdateState(nowPath));
            Finished.Invoke(new UpdateState(finished));
        }

        private void prgMerged()
        {
            prgMerge.Value = prgMerge.Maximum;
            btnStart.Enabled = true;
            btnpause.Enabled = false;
        }

        private void prgMerging()
        {
            prgMerge.Value++;
        }

        private void nowPath()
        {
            string tt = Resources[prgMerge.Value - 1].Substring(0, 3) + "---";
            tt += Resources[prgMerge.Value - 1].Substring(Resources[prgMerge.Value - 1].LastIndexOf("\\"));
            //string temp =Resources[prgMerge.Value-1].Remove(3,Resources[prgMerge.Value-1].LastIndexOf("\\")-Resources[prgMerge.Value-1].IndexOf("\\"));
            NowPath.Text = "当前文件：" + tt;
        }

        private void finished()
        {
            float i=((float)prgMerge.Value/(float)prgMerge.Maximum)*100;
            Finished.Text = "已完成：" + i.ToString()+"%";
        }
        

        private void MainFram_Load(object sender, EventArgs e)
        {

        }

        private void btnpause_Click(object sender, EventArgs e)
        {
            if (btnpause.Text.Trim()=="停止")
            {
                thread.Suspend();
                btnpause.Text = "继续";
            }
            else
            {
                thread.Resume();
                btnpause.Text = "停止";
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
